package com.buho29.rcmanager.bd;

import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;

public class TableModel extends BaseActiveRecord {

	public static final String SQL_TABLE_NAME = "model";
	
	public static final String SQL_CREATE_TABLE = "create table " + SQL_TABLE_NAME + " (" +
	    		"_id INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL, "+ 
	    		"name VARCHAR(20)  NOT NULL, " +
	    		"description VARCHAR(200) NULL, " +
	    		"date INTEGER NOT NULL, " +
				"accident_count INTEGER NOT NULL, " +
	    		"power_type INTEGER NOT NULL, " +
	    		"image VARCHAR(45) NULL" +
    		");";
	
	/*
	 * table column
	 */	
	public static final String COL_NAME = "name";
	public static final String COL_DESCRIPTION = "description";
	public static final String COL_DATE = "date";
	public static final String COL_ACCIDENT_COUNT = "accident_count";
	public static final String COL_POWER_TYPE = "power_type";
	public static final String COL_IMAGE = "image";
	//public static final String COL_ = "";
	
	public static final String[] COLUMNS = new String[] { 
		COL_ROW_ID, COL_NAME, COL_DESCRIPTION,COL_DATE ,
		COL_ACCIDENT_COUNT ,COL_POWER_TYPE,COL_IMAGE 
	};

	private String mName;
	public String getName() {return mName;}
	public void setName(String name) {mName = name;}
	
	private String mDescription;
	public String getDescription() {return mDescription;}
	public void setDescription(String description) {mDescription = description;}
	
	private long mDate;
	public long getDate() {return mDate;}
	
	private int mAccident_count;
	public int getAccident_count() {return mAccident_count;}
	public void setAccident_count(int accident_count) {mAccident_count = accident_count;}

	private int mPower_type;
	public int getPower_type() {return mPower_type;}
	public void setPower_type(int power_type) {mPower_type = power_type;}
	
	private String mImage;
	public String getImage() {return mImage;}
	public void setImage(String image) {mImage = image;}
	
	@Override
	public Boolean load() {
		Cursor cursor = mDb.query(true, SQL_TABLE_NAME,COLUMNS,
				COL_ROW_ID + "=" + mId,
				null, null,null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			loadCursor(cursor);
			cursor.close();
			return true;
		}
		return false;
	}

	@Override
	public long save() {
		ContentValues values = new ContentValues();
		values.put(COL_NAME,mName);
		values.put(COL_DESCRIPTION,mDescription);
		values.put(COL_ACCIDENT_COUNT,mAccident_count);
		values.put(COL_POWER_TYPE,mPower_type);
		values.put(COL_IMAGE,mImage);
		
		//values.put(COL_,);
		
		if (mId < 1) {
			values.put(COL_DATE,new Date().getTime());
			mId = mDb.insert(SQL_TABLE_NAME, null, values);
			return mId;
		} else { 
			values.put(COL_DATE,mDate);
			return mDb.update(SQL_TABLE_NAME, values, COL_ROW_ID + "=" + mId, null);
		}  
	}
	
	@Override
	public boolean delete() {
		return mDb.delete(SQL_TABLE_NAME, COL_ROW_ID + "=" + mId,     null) > 0;
	}
	
	@Override
	public Cursor retrieveAll() {
		return mDb.query(SQL_TABLE_NAME, COLUMNS, null, null,
				null, null, null);
	}
	@Override
	public void loadCursor(Cursor cursor) {
		mId = cursor.getLong(cursor.getColumnIndex(COL_ROW_ID));
		mName = cursor.getString(cursor.getColumnIndex(COL_NAME));
		mDescription = cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION));
		mDate = cursor.getLong(cursor.getColumnIndex(COL_DATE));
		mAccident_count = cursor.getInt(cursor.getColumnIndex(COL_ACCIDENT_COUNT));
		mPower_type = cursor.getInt(cursor.getColumnIndex(COL_POWER_TYPE));
		mImage = cursor.getString(cursor.getColumnIndex(COL_IMAGE));
	}
	
}
